package co.edu.eafit.analisisnumerico.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.table.DefaultTableModel;

import co.edu.eafit.analisisnumerico.framework.AnalisisException;
import co.edu.eafit.analisisnumerico.framework.Constantes;
import co.edu.eafit.analisisnumerico.framework.GestorInterfazPrincipalSistemasEcuaciones;
import co.edu.eafit.analisisnumerico.framework.GestorMetodos;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PopUpMISistemasEcuaciones.java
 *
 * Created on 1/05/2010, 03:42:45 PM
 */

/**
 *
 * @author nico
 */
public class PopUpMISistemasEcuaciones extends javax.swing.JFrame {

	GestorInterfazPrincipalSistemasEcuaciones gestor;
	int metodo;
	int tamano;
	
    /** Creates new form PopUpMISistemasEcuaciones */
    public PopUpMISistemasEcuaciones(final GestorInterfazPrincipalSistemasEcuaciones gestor, final int metodo, final Object[][] matriz) {
    	this.gestor=gestor;
    	this.metodo=metodo;
        initComponents();
        setVisible(true);
        tamano=matriz.length;
        Object[][] datos = new Object[1][matriz.length];
        String[] titulos = new String[matriz.length];
        for(int i=0;i<matriz.length;i++)titulos[i]="X"+(i+1);
        tblValoresIniciales.setModel(new DefaultTableModel(datos, titulos));
        btnRegresar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				PopUpMISistemasEcuaciones.this.setVisible(false);
			}
		});
        btnAceptar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				double[] valoresObtenidos = generarValoresObtenidos();
				if(valoresObtenidos==null)return;
				if(metodo==Constantes.JACOBI){
					GestorMetodos.ejecutarSistemaEcuacion(Constantes.JACOBI, "Jacobi", matriz, valoresObtenidos);
				}
				else if(metodo==Constantes.GAUSSSEIDEL){
					GestorMetodos.ejecutarSistemaEcuacion(Constantes.GAUSSSEIDEL, "Gauss Seidel", matriz, valoresObtenidos);
				}
			}
		});
    }

    protected double[] generarValoresObtenidos() {
    	double[] resul = new double[tamano+2];
    	try{
    		//POSICION 0-> TOLERANCIA
    		resul[0] = Double.parseDouble(txtTol.getText());
    		resul[1] = Double.parseDouble(txtIter.getText());
    		for(int i=0;i<tamano;i++){
    			resul[i+2]=Double.parseDouble(tblValoresIniciales.getValueAt(0, i).toString());
    		}
    		return resul;
    	}
    	catch(Exception e){
    		new AnalisisException("ENTRADA INVALIDA");
    		return null;
    	}
	}

	/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnAceptar = new javax.swing.JButton();
        btnRegresar = new javax.swing.JButton();
        txtIter = new javax.swing.JTextField();
        txtTol = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblValoresIniciales = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/co/edu/eafit/analisisnumerico/GUI/logo2.JPG"))); // NOI18N

        jLabel2.setText("Ingrese Tolerancia");

        jLabel3.setText("Ingrese numero de Iteraciones");

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnRegresar.setText("Regresar");
        btnRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegresarActionPerformed(evt);
            }
        });

        txtIter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIterActionPerformed(evt);
            }
        });

        txtTol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTolActionPerformed(evt);
            }
        });

        jLabel4.setText("Ingrese Valores Iniciales");

        tblValoresIniciales.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null}
            },
            new String [] {
                "Valores"
            }
        ));
        jScrollPane1.setViewportView(tblValoresIniciales);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAceptar)
                                .addGap(18, 18, 18)
                                .addComponent(btnRegresar))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtIter, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(77, 77, 77)
                                        .addComponent(txtTol, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)))
                                .addGap(66, 66, 66)))
                        .addGap(42, 42, 42)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(120, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtTol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtIter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAceptar)
                            .addComponent(btnRegresar))
                        .addContainerGap(45, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addGap(20, 20, 20))))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(240, 240, 240)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>

    private void txtIterActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}

    private void txtTolActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
}

    private void btnRegresarActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnRegresar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tblValoresIniciales;
    private javax.swing.JTextField txtIter;
    private javax.swing.JTextField txtTol;
    // End of variables declaration

}
